from mitmproxy import http

# 指定要捕获的 URL
target_url = "https://api-v2.xdclass.net/api/account/v1/login"


# 保存响应数据的文件
output_file = "response/filtered_responses.json"


def request(flow: http.HTTPFlow):
    print(f"Request URL: {flow.request.pretty_url}")
def response(flow: http.HTTPFlow):
    print(f"Response URL: {flow.request.pretty_url}")
    # 检查请求的 URL 是否匹配目标 URL
    if target_url in flow.request.pretty_url:
        # 只处理成功的响应
        if flow.response.status_code == 200:
            # 获取响应内容
            response_data = flow.response.text

            # 将数据保存到 JSON 文件
            with open(output_file, "a") as f:
                f.write(f"URL: {flow.request.pretty_url}\n")
                f.write(f"Response: {response_data}\n")
                f.write("-" * 50 + "\n")  # 分隔线

            print(f"Captured response from {flow.request.pretty_url}")